#include "slave_common.hpp"
#include <utility>

#ifdef min
#undef min
#endif


namespace dcs {

    auto float2u8Arry = [](uint8_t* u8Arry, float floatdata)
        {
            uint8_t farray[4];
            *(float*)farray = floatdata;

            u8Arry[0] = farray[0];
            u8Arry[1] = farray[1];
            u8Arry[2] = farray[2];
            u8Arry[3] = farray[3];
        };


    void buffer_update(modbus_mapping_t* buffer, std::uint16_t pt, std::time_t time, const std::vector<float>& params, const std::uint64_t &filename) {
        if (buffer == nullptr)
            return;

        std::uint32_t idx = 0;
        for (auto i = 0; i != params.size(); ++i)
        {
            float2u8Arry((uint8_t*)&buffer->tab_registers[idx], params[i]);
            idx += 2;
        }
        
        buffer->tab_registers[idx++] = pt;
        *(std::time_t*)&buffer->tab_registers[idx] = time;
        idx += 4;

        *(std::uint64_t*)&buffer->tab_registers[idx] = filename;
    }

}